import time
from main.model.logic.interruption import Interruption
from processing.shape.color import color



def cpu_run(listener, original, owner, *originalarg, **originalkarg):
    listener.cpu.run()
    original(owner, *originalarg, **originalkarg)
    listener.cpu.stop()
    
def cpu_run_instruction(listener, original, owner, *originalarg, **originalkarg):
    listener.cpu.run_instruction()
    original(owner, *originalarg, **originalkarg)
    
def pcb_to_ready(listener, original, owner, *originalarg, **originalkarg):
    listener.kernel.exit.send(listener.ready.enter, color.red)
    listener.ready.add_item()
    time.sleep(listener.delay)
    original(owner, *originalarg, **originalkarg)
    
def io_finish(listener, original, owner, *originalarg, **originalkarg):
    listener.io.exit.send(listener.kernel.enter, color.red)
    listener.io.remove_first_item()
    time.sleep(listener.delay)
    original(owner, *originalarg, **originalkarg)
    
def create_pcb(listener, original, owner, *originalarg, **originalkarg):
    listener.kernel.exit.send(listener.ready.enter, color.yellow)
    listener.ready.add_item(color.green)
    time.sleep(listener.creating_delay)
    original(owner, *originalarg, **originalkarg)
    
def handle_interruption(listener, original, owner, *originalarg, **originalkarg):
    listener.kernel.run()
    interruption = originalarg[0]
    if(interruption.kind != Interruption.EXECUTE):
        pass
    if(interruption.kind == Interruption.IO_REQUEST):
        listener.kernel.exit.send(listener.io.enter, color.yellow)
        listener.io.add_item(color.blue)
        time.sleep(listener.delay)
    original(owner, *originalarg, **originalkarg)
    listener.kernel.stop()
    
def load_pcb_to_cpu(listener, original, owner, *originalarg, **originalkarg):
    listener.ready.exit.send(listener.cpu.enter, color.blue)
    listener.ready.remove_first_item()
    original(owner, *originalarg, **originalkarg)
    
def set_kernel_mode(listener, original, owner, *originalarg, **originalkarg): 
    listener.background = color.black
    original(owner, *originalarg, **originalkarg)
    
def set_user_mode(listener, original, owner, *originalarg, **originalkarg):
    listener.background = color.green
    original(owner, *originalarg, **originalkarg)

def idle_mode(listener, original, owner, *originalarg, **originalkarg):
    listener.background = color.red
    time.sleep(listener.delay)
    original(owner, *originalarg, **originalkarg)
    
def cpu_trigger_interruption(listener, original, owner, *originalarg, **originalkarg):
    interruption_kind = originalarg[0]
    if(interruption_kind != Interruption.TIME_OUT):
        listener.cpu.exit.send(listener.kernel.enter, color.orange)
    if(interruption_kind != Interruption.KILL_PROCESS):
        listener.cpu.exit.send(listener.kernel.enter, color.black)
    if(interruption_kind != Interruption.IO_REQUEST):
        listener.cpu.exit.send(listener.kernel.enter, color.blue)
    original(owner, *originalarg, **originalkarg)
    